package com.atguigu.auth.controller;
import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.config.exception.GuiguException;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    //1.查询所有角色和当前所属角色
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        Map<String,Object> map = sysRoleService.findRoleDataByUserId(userId);
        return Result.ok(map);
    }

    //2.为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }



    @ApiOperation("查询所有角色")
    @GetMapping("findAll")
    public Result findAll(){
        List<SysRole> list = sysRoleService.list();
//        try {
//            int i = 1/0;
//        }catch (Exception e){
//            throw new GuiguException(20001,"执行了自定义异常处理");
//        }
        return Result.ok(list);
    }


    //添加角色
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole role) {
        System.out.println(role);

        //调用service的方法
        if (role.getRoleName() != null || role.getRoleCode() !=null){
            boolean is_success = sysRoleService.save(role);
            if(is_success) {
                return Result.ok();
            }
            else {
                return Result.fail();
            }
        }
        return Result.fail();
    }

    //修改角色-根据id查询
    //UPDATE sys_role SET is_deleted=1 WHERE id=? AND is_deleted=0 逻辑删除
    @ApiOperation("根据id查询")
    @GetMapping("getById/{id}")
    public Result get(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //修改角色-最终修改
    @ApiOperation("修改角色")
    @PutMapping ("update")
    public Result update(@RequestBody SysRole role) {
        //调用service的方法
        boolean is_success = sysRoleService.updateById(role);
        if(is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @DeleteMapping("removeById/{id}")
    public Result remove(@PathVariable Long id) {
        boolean is_success = sysRoleService.removeById(id);
        if(is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //批量删除    前端数组 [1,2,3]
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean is_success = sysRoleService.removeByIds(idList);
        if(is_success) {
            return Result.ok("批量删除成功");
        } else {
            return Result.fail("批量删除失败");
        }
    }





    //分页查询
    //条件分页查询
    //page 当前页  limit 每页显示记录数
    //SysRoleQueryVo 条件对象
//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/getPageList/{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo) {
        //1.创建Page对象，传递分页相关参数
//        Page<SysRole> pageParm = new Page<>(page,limit);
        Page<SysRole> pageParm = new Page<>();
        //2.封装条件
        LambdaQueryWrapper<SysRole> qw = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)){
            //封装  like模糊查询
            qw.like(SysRole::getRoleName,roleName);
        }

        //3.调用方法实现
        IPage<SysRole> sysRolePage = sysRoleService.page(pageParm, qw);
        System.out.println("=======总记录数==="+ sysRolePage.getTotal());
        return Result.ok(sysRolePage);
    }
}
